<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;
use Think\Page;
class KqbfController extends BaseController{
    public function View(){
         $year = date('Y');
        $b = $_GET['tex'];        
        $zl=0;
        $zr=0;              
        if ($b == null) {           
            $b= date("m");
            $t = date("t");           
            
        } else {
            if ($b == 9 || $b == 4 || $b == 6 || $b == 11) {
                $t = 30;
            } else if ($b == 1 || $b == 3 || $b == 5 || $b == 7 || $b == 8 || $b == 10 || $b == 12) {
                    $t = 31;
                } else if ($b == 2) {
                        if (($year % 4 == 0 && $year % 100 != 0) || ($year % 100 == 0 && $year % 400 == 0)) {
                            $t = 29;
                        } else {
                            $t = 28;
                        }
                    }
        }
        
        $days=$t;
        for ($i=1; $i<=$days; $i++) {
            $day = $year.'-'.$b.'-'.$i;
            $w = date('w',strtotime($day));
            if ($w == 6) {
                $zl+=1;
            }else if($w == 0){
                $zr+=1;
            }
        }
        
        $gxday=0.5*$zl+$zr;
        $resday=$t-$gxday;
        
        $this->assign('t', $resday);
        $a = $_GET['tex'];
        if ($a == null) {
            $a = date('m');
            if ($a < 10) {
                $a = substr($a, 1);
            }
        } else {
            $a = $_GET['tex'];
        }
        $this->assign('month', $a);
        $this->assign('null', null);
        $this->assign('title', '任务列表页面');
        $this->assign('info', '为出差人新增任务');
        
        $search = '%' . htmlspecialchars(I('get.search')) . '%';
        $where['staff_name'] = array(
            'like',
            $search
        );
        if ($search !== null) {
            $where['staff_name'] = array(
                'like',
                $search
            );
        }
            
        $where['month'] = $a;
        $ss = D('Ture');
        $count = $ss->where($where)->count();
        $page = new Page($count, 50);
        $page->setConfig('first', '首页');
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $page->setConfig('last', '末页');
        $page->rollPage = '6';
        $page->setConfig('theme', '%HEADER%  共    %TOTAL_PAGE% 页   %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $page->show();
        
        $tasklist = D('Ture')->where($where)
        ->limit($page->firstRow, $page->listRows)
        ->order('taskdate desc')
        ->select();
                
        $this->assign('page', $show);      
        $this->assign('tasklist', $tasklist);
        $year = date('Y');
        $this->assign('year', $year);
        $this->assign('monthlist', D('Month')->select());
        $this->assign('url', U('Task/search'));
               
        $this->display('Kqbf_list');
    }
    public function monthb()
    {
        if ($_GET['tex'] > 0) {
            $data['state'] = 1;
            $data['d'] = $_GET['tex'];
            $this->ajaxReturn($data);
        } else {
            $data['state'] = 0;
            $data['msg'] = '请求超时，请重试！';
            $this->ajaxReturn($data);
        }
    }
    
    public function searchb()
    {
        if ($_GET['tex'] > 0) {
            $data['state'] = 1;
            $data['id'] = $_GET['tex'];
            $data['search'] = $_GET['search'];
            $this->ajaxReturn($data);
        } else {
            $data['state'] = 0;
            $data['msg'] = '请求超时，请重试！';
            $this->ajaxReturn($data);
        }
    }
    
    public function ygdr(){
        $resultname=I('post.plstr');
      
        $where['name']=array('not in',$resultname);
        $map['staff_name']=array('in',$resultname);
        
        $turecount=D('Ture')->where($map)->count('id');
        $staffconut=D('Staff')->count('id');
        $count=D('Staff')->where($where)->count('id');
        $hj=$turecount+$count;
        
        $arr=D('Staff')->where($where)->select();
        if (empty($resultname)){
            $array=D('Staff')->select();
            foreach ($array as $v){
                $wmap['id']=$v['id'];
                $sname=D('Staff')->where($wmap)->find();
                $data['staff_name']=$sname['name'];
                $data['month']=I('post.monthid');
                D('Ture')->add($data);
            
            }
            
        }else {
            foreach ($arr as $v){
                $wmap['id']=$v['id'];
                $sname=D('Staff')->where($wmap)->find();
                $data['staff_name']=$sname['name'];
                $data['month']=I('post.monthid');
                D('Ture')->add($data);
            
            }
        }
                   
        if ($hj){
            $data['state'] = 1;
            $data['msg'] = '导入成功!请勿重复导入';
            $this->ajaxReturn($data);
        }else {
            $data['state'] = 0;
            $data['msg'] = '导入失败';
            $this->ajaxReturn($data);
        }
    }
    
    public function tdelete(){
        $id=I('post.wsj');    
        $map['id']=array('in',$id);
        $res=D('Ture')->where($map)->delete();
        if ($res){
            $data['state'] = 1;
            $data['msg'] = '删除成功';
            $this->ajaxReturn($data);
        }else {
            $data['state'] = 0;
            $data['msg'] = '删除失败';
            $this->ajaxReturn($data);
        }
    }
    
    
    
    public function edit(){
        $whm['id']=I('get.uid');
        $res=D('Ture')->where($whm)->find();   
        if ($res){
            $data['state'] = 1;
            $data['taskdate']=$res['taskdate'];
            $data['taskadd']=$res['taskadd'];
            $data['taskleave']=$res['taskleave'];
            $data['tasklate']=$res['tasklate'];
            $data['id']=$res['id'];
            $data['staff_name']=$res['staff_name'];
            $this->ajaxReturn($data);
        }else{
            $data['state'] = 0;
            $data['msg'] = '请求失败';
            $this->ajaxReturn($data);
        }

    }
   
    public function editsure(){
        $data['taskdate']=I('post.taskdate');
        $data['taskadd']=I('post.taskadd');
        $data['taskleave']=I('post.taskleave');
        $data['tasklate']=I('post.tasklate');
        $where['id']=I('post.etid');
        $result=D('Ture')->where($where)->save($data);
        if (false !== $result || 0 !== $result){
            $data['state'] = 1;
            $data['msg'] = '修改成功';
            $this->ajaxReturn($data);
        }else {
            $data['state'] = 0;
            $data['msg'] = '修改失败';
            $this->ajaxReturn($data);
        }
    }
    
    public function total()
    {
        $where['id'] = I('get.id');
        $tureresult = D('Ture')->where($where)->find();
        $map['name'] = $tureresult['staff_name'];
        $staffsrult = D('Staff')->where($map)->find();

        $d = date('t');//当月天数
        $salary = $staffsrult['salary']; // 基本工资
        $social = $staffsrult['social']; // 社保费
        $taskdate = $tureresult['taskdate']; // 出差天数
        $taskadd = $tureresult['taskadd']; // 加班天数
        $taskleave = $tureresult['taskleave']; // 请假天数
        $tasklate = $tureresult['tasklate']; // 迟到次数
        if ($tasklate == 1) {
            $resultsalary = $salary + $taskdate * 60 + $taskadd * ($salary / $d) - $taskleave * ($salary / $d) - 100 - $social;
        } else
                if ($tasklate > 1) {
                    $tas = ($tasklate - 1) * 20;
                    $resultsalary = $salary + $taskdate * 60 + $taskadd * ($salary / $d) - $taskleave * ($salary / $d) - 100 - $tas - $social;
                } else {
                    $resultsalary = $salary + $taskdate * 60 + $taskadd * ($salary / $d) - $taskleave * ($salary / $d) - $social;
                }
            $data['tsalary'] = round($resultsalary,2);
            if (D('Ture')->where($where)->save($data)) {
                $data['state'] = 1;
                $data['msg'] = '计算成功';
                $this->ajaxReturn($data);
            } else {
                $data['state'] = 0;
                $data['msg'] = '计算失败请重试';
                $this->ajaxReturn($data);
            }
        }
        
       
public function pltotal()
{
    $b = I('post.checkedList');     
    foreach ($b as $v) {           
        $where['id'] = $v;
        $tureresult = D('Ture')->where($where)->find();
        $map['name'] = $tureresult['staff_name'];
        $staffsrult = D('Staff')->where($map)->find();

        $d = date('t');//当月天数
        $salary = $staffsrult['salary']; // 基本工资
        $social = $staffsrult['social']; // 社保费
        $taskdate = $tureresult['taskdate']; // 出差天数
        $taskadd = $tureresult['taskadd']; // 加班天数
        $taskleave = $tureresult['taskleave']; // 请假天数
        $tasklate = $tureresult['tasklate']; // 迟到次数
        if ($tasklate == 1) {
            $resultsalary = $salary + $taskdate * 60 + $taskadd * ($salary / $d) - $taskleave * ($salary / $d) - 100 - $social;
        } else
                if ($tasklate > 1) {
                    $tas = ($tasklate - 1) * 20;
                    $resultsalary = $salary + $taskdate * 60 + $taskadd * ($salary / $d) - $taskleave * ($salary / $d) - 100 - $tas - $social;
                } else {
                    $resultsalary = $salary + $taskdate * 60 + $taskadd * ($salary / $d) - $taskleave * ($salary / $d) - $social;
                }
            $data['tsalary'] = round($resultsalary,2);
            $result = D('Ture')->where($where)->save($data); 
           
        }  
         if ($result){
             $data['state'] = 1;
             $data['msg'] = '计算成功';
             $this->ajaxReturn($data);
         }else {
             $data['state'] = 0;
             $data['msg'] = '计算失败请重试';
             $this->ajaxReturn($data);
         }
      
      
    }
       
            
            
            
    
    
    
}